#include "MCLinearDimension2D.h"

MCLinearDimension2D::MCLinearDimension2D() :
    _sourceLine(MCPoint2D(), MCPoint2D()),
    _height(0),
    _alligment(MCLinearDimensionAlligment::DefaultAlligment)
{
}

MCLinearDimension2D::MCLinearDimension2D(const MCPoint2D &p1, const MCPoint2D &p2) :
    _sourceLine(p1, p2),
    _height(0),
    _alligment(MCLinearDimensionAlligment::DefaultAlligment)
{
}

MCDimensionType MCLinearDimension2D::dimensionType()
{
    return MCDimensionType::Linear;
}

double MCLinearDimension2D::length()
{
    return dimensionLine().length();
}

double MCLinearDimension2D::height() const
{
    return _height;
}

void MCLinearDimension2D::setAlligment(MCLinearDimensionAlligment alligment)
{
    _alligment = alligment;
}

void MCLinearDimension2D::setHeight(double height)
{
    _height = height;
}

void MCLinearDimension2D::moveDimensionLine(const MCVector2 &to)
{
    MCVector2 normal = dimensionLine().normal();

}

MCLinearDimensionAlligment MCLinearDimension2D::alligment() const
{
    return _alligment;
}

MCLineSegment2D MCLinearDimension2D::dimensionLineNoTransform() const
{
    return getAllignedLine(_alligment);
}

MCLineSegment2D MCLinearDimension2D::dimensionLine() const
{
    MCLineSegment2D dimensionLine = getAllignedLine(_alligment);
    dimensionLine.move(dimensionLine.normal() * _height);

    return dimensionLine;
}

MCLineSegment2D MCLinearDimension2D::inversedDimensionLine() const
{
    MCLineSegment2D dimensionLine = getAllignedLine(_alligment);
    MCVector2 normal = dimensionLine.normal() * -1;
    dimensionLine.move(normal * _height);

    return dimensionLine;
}

MCLineSegment2D MCLinearDimension2D::sourceLine() const
{
    return _sourceLine;
}

void MCLinearDimension2D::update(const MCPoint2D &p1, const MCPoint2D &p2)
{
    _sourceLine = MCLineSegment2D(p1, p2);
}

MCLineSegment2D MCLinearDimension2D::getAllignedLine(MCLinearDimensionAlligment alligment) const
{
    MCLineSegment2D result;

    switch (alligment)
    {
    case DefaultAlligment:
        result = defaultAlligmentLine(_sourceLine);
        break;
    case XAlligment:
        result = XAlligmentLine(_sourceLine);
        break;
    case YAlligment:
        result = YAlligmentLine(_sourceLine);
        break;
    default:
        break;
    }

    return result;
}

MCLineSegment2D MCLinearDimension2D::defaultAlligmentLine(const MCLineSegment2D &seg) const
{
    return MCLineSegment2D(seg);
}

MCLineSegment2D MCLinearDimension2D::XAlligmentLine(const MCLineSegment2D &seg) const
{
    MCPoint2D p1(seg.startPoint());
    MCPoint2D p2(seg.startPoint().x, seg.endPoint().y);
    return MCLineSegment2D(p1, p2);
}

MCLineSegment2D MCLinearDimension2D::YAlligmentLine(const MCLineSegment2D &seg) const
{
    MCPoint2D p1(seg.startPoint());
    MCPoint2D p2(seg.endPoint().x, seg.startPoint().y);
    return MCLineSegment2D(p1, p2);
}
